package com.yanftch.hdc_tools.command

import com.yanftch.hdc_tools.common.MessageDelegate
import com.yanftch.hdc_tools.common.executeCommand
import com.yanftch.hdc_tools.common.finalCommandLine
import com.yanftch.hdc_tools.common.isCommandSucceed
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking

class CleanDataCommand : Command {
    override fun run(commandContext: CommandContext): Boolean {
        with(commandContext) {
            try {
                runBlocking {
                    val clean_c = async { executeCommand(finalCommandLine(hdcPath, hdc_bm_clean_c(bundleName))) }.await()
                    val clean_d = async { executeCommand(finalCommandLine(hdcPath, hdc_bm_clean_d(bundleName))) }.await()
                    if (clean_c.isCommandSucceed() && clean_d.isCommandSucceed()) {
                        MessageDelegate.info("clean data success.", action)
                    } else {
                        MessageDelegate.error("clean data failed. $clean_c: $clean_d", action)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
                MessageDelegate.error("clean data failed. ${e.message}", action)
            }
        }
        return false
    }
}